/*
 *
 * PermissionUtils.java
 * 
 * Created by Wuwang on 2016/11/14
 * Copyright © 2016年 深圳哎吖科技. All rights reserved.
 */
package com.wk.camerapreviewwithfilter.utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

public class PermissionUtils {


    public static void askPermission(Activity context, String[] permissions, int req, Runnable
            runnable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean hasPermission = true;
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) == PackageManager
                        .PERMISSION_DENIED) {
                    hasPermission = false;
                    break;
                }
            }
            if (hasPermission) {
                runnable.run();
            } else {
                ActivityCompat.requestPermissions(context, permissions, req);
//                if(context instanceof AppCompatActivity){
//                    context.requestPermissions(permissions,req);
//                }else {
//                    ActivityCompat.requestPermissions(context, permissions, req);
//                }
            }
        } else {
            runnable.run();
        }
    }

    public static void onRequestPermissionsResult(boolean isReq, int[] grantResults, Runnable
            okRun, Runnable deniRun) {
        if (isReq) {
            if (grantResults.length > 0) {
                for (int grantResult : grantResults) {
                    if (grantResult != PackageManager.PERMISSION_GRANTED) {
                        deniRun.run();
                        return;
                    }
                }
                okRun.run();
            }
        }
    }

}
